package joc.internal.transformer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javassist.CtClass;
import javassist.CtField;

public abstract class AccessibleFieldTransformer extends ClassTransformer {

	public AccessibleFieldTransformer(Transformer transformer) {
		super(transformer);
	}

	@Override
	public void transform(CtClass clazz) throws Exception {
		for (CtField field : getAccessibleFields(clazz)) {
			transform(clazz, field);
		}
	}

	private List<CtField> getAccessibleFields(CtClass clazz) {
		List<CtField> fields = new ArrayList<CtField>(Arrays.asList(clazz.getDeclaredFields()));
		for (CtField nonPrivateField : clazz.getFields()) {
			if (!fields.contains(nonPrivateField)) {
				fields.add(nonPrivateField);
			}
		}
		return fields;
	}

	public abstract void transform(CtClass clazz, CtField field) throws Exception;
}
